import { defineStore } from 'pinia'
// @ts-ignore
// import {ElLoading} from "element-plus";
import {getCurrentInstance} from "vue";
import {ElMessage} from "element-plus";
// @ts-ignore

export default defineStore('storeIdA', {
    // 其他配置...
    state: ()=>{
        return{
            radio: '全部订单',
            list: [],
            searchListA: []
        }
    },
    getters: {
        dynamic():any{
            // @ts-ignore
            if(this.radio === '全部订单'){
                return this.searchListA = this.list
            }else{
                // @ts-ignore
                return this.searchListA = this.list.filter((item:object)=> item.state ===this.radio)
            }
        }
    },
    // 同步异步统一到actions中了
    actions: {
        alterA(val: string): void {
            this.radio = val
            console.log(this.radio)
        },
        async request() {
            let {proxy}: any = getCurrentInstance()
            let {data: {data}} = await proxy.$axios.get('/json/list.json')
            console.log(data)
            this.list = data
            this.searchListA = this.list
        },
        editA(obj: any, key: number) {
            this.list.some((item: any) => {
                if (item.key === key) {
                    item.commodity = obj.commodity
                    item.price = obj.price
                    item.amount = obj.amount
                    item.state = obj.state
                    item.quantity = obj.quantity
                    item.CreationTime = obj.CreationTime
                    return true
                }
            })
            ElMessage({
              type: 'success',
              message: '修改成功'
            })
        },
        // 商品列表删除数据
        filterB(key: number): void {
            this.list = this.list.filter((item: any): boolean => item.key !== key)
            this.searchListA = this.list
            ElMessage({
                type: 'success',
                message: '删除成功'
            })
        },
        // 搜索功能
        searchA(val:any, v){
            // console.log(val.key)
            this.searchListA = this.list.filter((item:any)=>{
                if(val.key===''&&val.name===''&&val.time===''){
                    return this.searchListA = this.list, this.radio = '全部订单'
                }else{
                    return item.key === val.key ||item.commodity === val.name || item.CreationTime ===val.time
                }
            })
            if(this.searchListA.length===0){
                ElMessage({
                    type: 'warning',
                    message: '没有对应的商品'
                })
            }else{
                if(v===1){
                    ElMessage({
                        type: 'success',
                        message: '重置按钮'
                    })
                    return
                }
                ElMessage({
                    type: 'success',
                    message: '搜索的结果，对应在表格中'
                })
            }
        }
    }
})

